<?php
if( !defined('IN_INDEX') )
{
	posterror("ACCESS DENIED", "This page is hidden.");
	exit;
}
if(checkSession())
{
	function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=profile");
	exit;
}

function getSettings($setting)
{
	global $db;
	
	$sql = "SELECT ". $setting ." FROM settings";
	if(!$result = $db->sql_query($sql))
	{
		printError('SQL_ERROR', mysql_error($sql));
	}
	else
	{
		while($row = $db->sql_fetchrow($result))
		{
			$value = $row[$setting];		
		}
		
	}
	return $value;
}

$preselected_country = $VISITOR_COUNTRY;
$COUNTRY_LIST = get_country_list();

$reg['firstname'] = "";
$reg['lastname']="";
$reg['email']="";
$reg['password']="";
$reg['country']="";
$reg['gender']="";
$reg['day']="";
$reg['month']="";
$reg['year']="";
$reg['unit']="";

$GENDER_LIST = array('male', 'female');
$array_unit = array("kg" => "kilogram", "lbs" => "pounds");
$missing = array();
$output ="";
if(isset($_POST['reg_submit']))
{
	$reg_checked = 0;
	
	$reg['firstname']=$_POST['reg_firstname'];
	$reg['lastname']=$_POST['reg_lastname'];
	$reg['email']=$_POST['reg_email'];
	$reg['password']=$_POST['reg_pass'];
	$reg['country']=$_POST['country_list'];
	$reg['gender']=$_POST['reg_gender'];
	$reg['day']=$_POST['reg_bday_day'];
	$reg['month']=$_POST['reg_bday_month'];
	$reg['year']=$_POST['reg_bday_year'];
	$reg['unit']=$_POST['reg_unit'];
	$reg['invitecode']=$_POST['reg_invitecode'];
	
	if($reg['country'] != "")
	{
		$preselected_country = $reg['country'];
	}
	foreach($reg as $k => $v)
	{
		//echo $k . " | " . $v . "<br />\n";
		
		if(isset($v) && $v != "")
		{
			if($k == "email")
			{
				if(strstr($v, '@') && strstr($v, '.'))
				{
					if(checkUserExistance('email', $v))
					{
						$missing['email'] = "is_regged";
					}
					else
					{
						$reg_checked++;
					}
				}
				else
				{
					$missing['email'] = "not_email";
				}
			}
			else
			{
				$reg_checked++;
			}
		}
		else
		{
			$missing[$k] = "missing";
		}
	}
	
	if($reg_checked >= 11)
	{
		
	//invitecode START
	$fetchcode = $_POST['reg_invitecode'];
	if($fetchcode != getSettings('settings_invitecode'))
	{
	echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><center><h1>" . translate('notinvited') . "</h1><br />
	" . translate('getinvite') . "</center>";
	echo "<div class='fixed'>
					</div>
				</div>
				</div>
			<div class='fixed'>
			</div>
			<div id='footer'>
			<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
				<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
                </div>";
		exit;
	} 
	//invitecode END
		
		$birthday = $reg['day'] . "-" . $reg['month'] . "-" . $reg['year'];
		$sql = "INSERT INTO " . USERS_TABLE . " SET ".
		"user_email='" . $reg['email'] . "', ".
		"user_password='" . cryptString($reg['password']) . "', ".
		"user_birthday='" . $birthday . "', ".
		"user_gender='" . $reg['gender'] . "', ".
		"user_lang='" . strtolower($reg['country']) . "', ".
		"user_country='" . $reg['country'] . "', ".
		"user_firstname='" . $reg['firstname'] . "', ".
		"user_lastname='" . $reg['lastname'] . "', ".
		"user_sort_comments='comment_date', ".
		"user_weight_metric='". $reg['unit'] ."', ".
		"user_invitecode='". $reg['invitecode'] ."', ".
		"user_joined='" . time() . "'";
		if(!$result = $db->sql_query($sql))
		{
			printError("SQL ERROR", mysql_error($sql) ."<br />" . mysql_errno($sql));
		}
		else
		{	
		echo"<br /><br /><br /><br /><br /><br /><br /><br /><br /><center><meta http-equiv=\"refresh\"content=\"3;url=index.php\">";
		echo "<br /><br /><h1>" . ucfirst(translate('regcomplete')) . "!</h1>\n";
		echo "<br/>" . translate('welcometo'). " <a href=\"index.php\"><br /><br />" . translate('click_to_login') . "</a>.</center>
		<div class='fixed'>
					</div>
				</div>
				</div>
			<div class='fixed'>
			</div>
			<div id='footer'>
			<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
				<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
                </div>";
		exit;
		}
	}
}
?>

<table  align="center" style="padding-left:40px; padding-right:40px;" cellspacing="0" border="0" width="100%">
			    <tr>
                
                <h1><img src="img/people.png" alt="reg" /> <?php echo ucfirst(translate('reg_header')); ?></h1><br />
<p><?php echo ucfirst(translate('reg_subtext')) . ""; ?></p>
<br />
<table>
<tr>
<td valign="top" width="45%">
<b>
<form method ="post" action="index.php?action=login">
	<table class="reg_table" border="0" cellspacing="0">
    	<tr>
        <td colspan="2" style="background-color: #F2F2F2; font-size:14px; padding: 5px;"><b><?php echo translate('accountinformation'); ?></b></td></tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
    	<tr>
        	<td class="reg_label"><?php echo ucfirst(translate('firstname')); ?>: </td>
            <td><input type="text" class="inputtext" size="25" id="reg_firstname" name="reg_firstname" value="<?php echo $reg['firstname']; ?>" />
				<?php 
                if(isset($missing['firstname'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['firstname']) . "</span>";
                }
                ?>
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
        <tr>
        	<td class="reg_label"><?php echo ucfirst(translate('lastname')); ?>: </td>
            <td><input type="text" class="inputtext" size="25" id="reg_lastname" name="reg_lastname" value="<?php echo $reg['lastname']; ?>" />
				<?php 
                if(isset($missing['lastname'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['lastname']) . "</span>";
                }
                ?>
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
        <tr>
        	<td class="reg_label"><?php echo ucfirst(translate('country')); ?>: </td>
            <td><?php include('inc/country_select.php'); ?>
            	<?php 
                if(isset($missing['country'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['country']) . "</span>";
                }
                ?>
                </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
        <tr>
        	<td class="reg_label"><?php echo ucfirst(translate('choosegender')); ?>: </td>
            <td>
            	<select class="select" id="reg_gender" name="reg_gender">
                	<option value=""><?php echo ucfirst(translate('iamgender')); ?></option>
					<?php
					foreach($GENDER_LIST as $k => $v)
					{
						if($v == $reg['gender'])
						{
							echo "<option value=\"" . $v . "\" selected=\"selected\">" . ucfirst(translate($v)) . "</option>\n";
						}
						else
						{
							echo "<option value=\"" . $v . "\">" . ucfirst(translate($v)) . "</option>\n";
						}
					}
					?>
            	</select>
                <?php 
                if(isset($missing['gender'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['gender']) . "</span>";
                }
                ?>
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
        <tr>
        	<td class="reg_label"><?php echo ucf(translate('birthday')); ?>: </td>
            <td>
            	<select class="select" id="reg_bday_day" name="reg_bday_day">
                	<option value=""><?php echo ucf(translate('day')); ?></option>
					<?php
					for($i=1; $i<=31; $i++)
					{
						if($i == $reg['day'])
						{
							echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
						}
						else
						{
							echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
						}
					}
					?>
            	</select>
                <select class="select" id="reg_bday_month" name="reg_bday_month">
           	    <option value=""><?php echo ucf(translate('month')); ?></option>
					<?php
					foreach($MONTHS as $k => $v)
					{
						if(($k+1) == $reg['month'])
						{
							echo "<option value=\"" . ($k+1) . "\" selected=\"selected\">" . ucf(translate($v)) . "</option>\n";
						}
						else
						{
							echo "<option value=\"" . ($k+1) . "\">" . ucf(translate($v)) . "</option>\n";
						}
					}
					?>
            	</select>
                <select class="select" id="reg_bday_year" name="reg_bday_year">
           	    <option value=""><?php echo ucf(translate('year')); ?></option>
                    <?php
					for($i=date('Y', time()); $i>=1900; $i--)
					{
						if($i == $reg['year'])
						{
							echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
						}
						else
						{
							echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
						}
					}
					?>
            	</select>
                <?php 
				if(isset($missing['day']) or isset($missing['month']) or isset($missing['year']))
				{
					echo "<span class=\"error\"><-- " . translate('missing') . "</span>";
				}
                ?>
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
        <tr>
        <td class="reg_label"><?php echo ucf(translate('view')); ?>: </td>
            <td>
            <?php
					foreach($array_unit as $key => $val)
					{
						if($val == "lbs") 
						{ 
							//echo " or "; 
						}
						
						echo "  <input type=\"radio\" name=\"reg_unit\" value=\"" . $key . "\"";
						if($key == $reg['unit'])
						{
							echo " checked=\"checked=\"";
						}
						echo " > " . ucf(translate($val)) . "  ";
					}
					?>
            <?php 
                if(isset($missing['unit'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['unit']) . "</span>";
                }
                ?>
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
        <tr><td colspan="2" style="background-color: #F2F2F2; font-size:14px; padding: 5px;"><b><?php echo translate('logininformation'); ?></b></td></tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
    	<tr>
        	<td class="reg_label"><?php echo ucfirst(translate('email')); ?>: </td>
            <td><input type="text" class="inputtext" size="25" id="reg_email" name="reg_email" value="<?php echo $reg['email']; ?>" />
            	<?php 
                if(isset($missing['email'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['email']) . "</span>";
                }
                ?>
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
        <tr>
        	<td class="reg_label"><?php echo ucfirst(translate('password')); ?>: </td>
            <td><input type="password" class="inputtext" size="25" id="reg_pass" name="reg_pass" value="" />
            	<?php 
                if(isset($missing['password'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['password']) . "</span>";
                }
                ?>
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
        <tr><td colspan="2" style="background-color: #F2F2F2; font-size:14px; padding: 5px;"><b><?php echo translate('invitecodeheader'); ?></b></td></tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
        <tr>
        	<td class="reg_label"><?php echo ucfirst(translate('invitecode')); ?>: </td>
            <td><input type="text" class="inputtext" size="25" id="reg_invitecode" name="reg_invitecode" value="" />
            <a href="mailto:feedback@thepowerlifters.com?Subject=<?php echo translate('iwantcode'); ?>"><?php echo translate('requestcode'); ?></a>
            	<?php 
                if(isset($missing['invitecode'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['invitecode']) . "</span>";
                }
                ?>
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
         <tr>
         	<td></td>
        	<td>
            <input style="font-weight:bold;" type="submit" id="reg_submit" name="reg_submit" value="<?php echo ucfirst(translate('sign_up')); ?>" />
            </td>
        </tr>
	</table>
</form></b>  
			        </td>
                    <td width="55%" style="padding-left:90px;" align="left">
                    <h2><?php echo translate('frontp0'); ?></h2>
                    <br />
					            <li><?php echo translate('frontp1'); ?></li>
                                <br />
					            <li><?php echo translate('frontp2'); ?></li>
                                <br />
					            <li><?php echo translate('frontp3'); ?></li>
					            <br />
                                <li><?php echo translate('frontp4'); ?></li>
                                <br />
                                <li><?php echo translate('frontp5'); ?></li>
                            <br />	
                    
            <h2><?php echo ucf(translate('factsaboutthistraining')); ?></h2>
          <br />
         <?php echo translate('sofar'); ?> <b><?php echo countUsers(); ?> <?php echo translate('members'); ?></b>  <?php echo strtolower(translate('lifted'));?> <b><?php echo countKgLifted() ." kg" ?></b>  
		 <br />
		 <?php echo ucf(translate('on'));?> <b><?php echo countTrainings(); ?> <?php echo translate('trainings'); ?></b> <?php echo translate('distributed'); ?> <?php echo translate('on');?>  <b><?php echo countSets(); ?> <?php echo translate('set');?></b> <?php echo translate('and'); ?> <b><?php echo countReps(); ?> <?php echo translate('reps');?></b>
         <br /> 
        <?php echo ucf(translate('and')); ?> <?php echo translate('ithas'); ?> <?php echo translate('they'); ?> <?php echo translate('used'); ?> <b><?php echo countTimeHours(); ?> <?php echo strtolower(translate('hours')); ?></b> <?php echo translate('on2'); ?>.<br />
        <img src="img/front_graf.jpg" />
                    </td>
			    </tr>
</table>